<template>
	<view class="container">
		<view class="address-list" v-if="addressList.length > 0">
			<view v-for="(item, index) in addressList" :key="index" class="item" @tap="addressAddOrUpdate" :data-address-id="item.id">
				<view class="address-header">
					<view class="name">
						<view>{{item.name}}</view>
						<view class="default" v-if="item.isDefault">默认</view>
					</view>
					<view class="remove" @tap.stop="deleteAddress" :data-address-id="item.id">删除</view>
				</view>
				<view class="address">{{item.detailedAddress}}</view>
				<view class="mobile">手机号码：{{item.mobile}}</view>
			</view>
		</view>
		<view class="empty-view" v-if="addressList.length <= 0">
			<image class="icon" src="/static/static/images/noAddress.png"></image>
			<text class="text">暂无收获地址哦～</text>
		</view>
		<view class="btn-global-orange" @tap="addressAddOrUpdate" data-address-id="0">新建地址</view>
	</view>
</template>

<script>
const util = require("../../../utils/util.js");
const api = require("../../../config/api.js");
const app = getApp();

export default {
	data()
	{
		return {
			addressList: []
		};
	},

	components: {},
	props: {},
	onLoad: function (options)
	{// 页面初始化 options为页面跳转所带来的参数
	},
	onReady: function ()
	{// 页面渲染完成
	},
	onShow: function ()
	{
		// 页面显示
		this.getAddressList();
	},
	onHide: function ()
	{// 页面隐藏
	},
	onUnload: function ()
	{// 页面关闭
	},
	methods: {
		getAddressList()
		{
			let that = this;
			util.request(api.AddressList).then(function (res)
			{
				if (res.errno === 0)
				{
					that.setData({
						addressList: res.data
					});
				}
			});
		},

		addressAddOrUpdate(event)
		{
			console.log(event); //返回之前，先取出上一页对象，并设置addressId

			let pages = getCurrentPages();
			let prevPage = pages[ pages.length - 2 ];

			if (prevPage && prevPage.route == "pages/checkout/checkout")
			{
				try
				{
					uni.setStorageSync('addressId', event.currentTarget.dataset.addressId);
				} catch (e) { }

				let addressId = event.currentTarget.dataset.addressId;

				if (addressId && addressId != 0)
				{
					uni.navigateBack();
				} else
				{
					uni.navigateTo({
						url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
					});
				}
			} else
			{
				uni.navigateTo({
					url: '/pages/ucenter/addressAdd/addressAdd?id=' + event.currentTarget.dataset.addressId
				});
			}
		},

		deleteAddress(event)
		{
			let that = this;
			uni.showModal({
				title: '',
				content: '确定要删除地址？',
				success: function (res)
				{
					if (res.confirm)
					{
						let addressId = event.target.dataset.addressId;
						util.request(api.AddressDelete, {
							id: addressId
						}, 'POST').then(function (res)
						{
							if (res.errno === 0)
							{
								that.getAddressList();
								uni.removeStorage({
									key: 'addressId',
									success: function (res) { }
								});
							}
						});
						console.log('用户点击确定');
					}
				}
			});
			return false;
		}

	}
};
</script>
<style>
page {
	height: 100%;
	width: 100%;
	background: #f4f4f4;
}

.container {
	height: 100%;
	width: 100%;
}

.address-list {
	margin-bottom: 60rpx;
	padding: 30rpx;
}

.address-list .item {
	background-color: #fff;
	margin-bottom: 30rpx;
	border-radius: 8rpx;
	overflow: hidden;
	/* box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.1); */
	padding: 30rpx;
	position: relative;
}

.address-header {
	display: flex;
	justify-content: space-between;
	margin-bottom: 15rpx;
}

.address-list .name {
	height: 43rpx;
	font-size: 30rpx;
	color: #212223;
	text-overflow: ellipsis;
	white-space: nowrap;
	overflow: hidden;
	display: flex;
	align-items: center;
}

.address-list .remove {
	font-size: 30rpx;
	color: #fd4d36;
}

.address-list .default {
	height: 35rpx;
	padding: 0 10rpx;
	line-height: 35rpx;
	text-align: center;
	font-size: 20rpx;
	color: #fd4d36;
	background-color: rgba(253, 77, 54, 0.3);
	display: inline-block;
	margin-left: 15rpx;
	border-radius: 4rpx;
}

.address-list .c {
	flex: 1;
	height: auto;
	overflow: hidden;
}

.address-list .delete {
	font-size: 28rpx;
	color: #fd4d36;
}

.address-list .mobile {
	font-size: 28rpx;
	color: #898b8d;
}

.address-list .address {
	font-size: 28rpx;
	color: #898b8d;
}

.address-list .r {
	width: 52rpx;
	height: auto;
	overflow: hidden;
	margin-right: 16.5rpx;
}

.address-list .del {
	display: block;
	width: 52rpx;
	height: 52rpx;
}

.btn-global-orange {
	width: 690rpx;
	height: 90rpx;
	border-radius: 45rpx;
	font-size: 35rpx;
	color: #fff;
	text-align: center;
	display: flex;
	justify-content: center;
	align-items: center;
	opacity: 1;
	background: linear-gradient(124deg, #f54c56 17%, #f95d20 83%);
	margin: 0 auto;
}

.empty-view {
	height: 100vw;
	width: 100%;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	box-sizing: border-box;
	padding-top: 40vw;
}

.empty-view .icon {
	height: 296rpx;
	width: 610rpx;
}

.empty-view .text {
	width: auto;
	font-size: 28rpx;
	line-height: 35rpx;
	color: #c1c1c1;
	transform: translate(20rpx, -50rpx);
}
</style>